package work.jame.blog.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import work.jame.blog.framework.VerificationGroup;
import work.jame.blog.framework.result.Result;
import work.jame.blog.pojo.UserCollectGroup;
import work.jame.blog.pojo.vo.ArticleCollectGroupVO;
import work.jame.blog.service.UserCollectGroupService;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jame
 * @since 2023-05-18
 */
@Api(tags = "用户收藏组")
@RestController
@RequestMapping("/userCollectGroup")
public class UserCollectGroupController {

    @Autowired
    private UserCollectGroupService userCollectGroupService;


    @ApiOperation("添加组")
    @PostMapping("/save")
    public Result<?> save(@Validated(VerificationGroup.Save.class) @RequestBody UserCollectGroup userCollectGroup) {
        if (userCollectGroupService.saveGroup(userCollectGroup)) {
            return Result.succeed(userCollectGroup.getUserId());
        }
        return Result.fail();
    }

    @ApiOperation("修改组")
    @PostMapping("/update")
    public Result<?> update(@Validated(VerificationGroup.Update.class) @RequestBody UserCollectGroup userCollectGroup) {
        if (userCollectGroupService.updateGroupById(userCollectGroup)) {
            return Result.succeed();
        }
        return Result.fail();
    }

    @ApiOperation("删除组")
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam("groupId") Long groupId) {
        if (userCollectGroupService.deleteCollectGroup(groupId)) {
            return Result.succeed();
        }
        return Result.fail();
    }


    @ApiOperation("获取所有组")
    @GetMapping("/list")
    public Result<?> list(@RequestParam("articleId")Long articleId){
        List<ArticleCollectGroupVO> list = userCollectGroupService.getCurrentUserCollectGroups(articleId);
        if(list.isEmpty()){
            return Result.noData();
        }
        return Result.succeed(list);

    }

}
